<?php
/**
 * Created by PhpStorm.
 * User: Dick
 * Date: 2016/4/13
 * Time: 15:54
 */

namespace Model;

use Method\JSON;

class Response
{
    public $id;
    public $who;
    public $content;
    public $tocomment;
    public $datetime;
    public $towho;

    public function SendResponse()
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $this->towho = User::GetIdByUsername($this->towho);
        if (!$this->isVerify()) {
            $statement = $pdo->prepare("INSERT INTO response SET who=:who,content=:content,tocomment=:tocomment,datetime=:datetime,towho=:towho");
            $statement->bindValue(':who', $this->who);
            $statement->bindValue(':content', $this->content);
            $statement->bindValue(':tocomment', $this->tocomment);
            $statement->bindValue(':datetime', $this->datetime);
            $statement->bindValue(':towho', $this->towho);
            $statement->execute();

            $huifu = $pdo->prepare("SELECT hfnum FROM mood WHERE id=?");
            $huifu->bindValue(1, $this->tocomment);
            $huifu->execute();
            $result = $huifu->fetch();
            $hfnum = intval($result['hfnum']);
            $hfnum += 1;
            $hf = $pdo->prepare("UPDATE mood SET hfnum=:hfnum WHERE id=:id");
            $hf->bindValue(':hfnum', $hfnum);
            $hf->bindValue(':id', $this->tocomment);
            $hf->execute();
            if (!$inTransaction) {
                $pdo->commit();
            }
            echo $hfnum;
        } else {
            return;
        }
    }

    public static function GetResponse($tocomment, $page = array('*'))
    {
        global $pdo;
        $start = $page['dataStart'];
        if ($start == null) {
            $start = 0;
        }
        $size = $page['pageSize'];
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statement = $pdo->prepare("SELECT response.*,user.username,user.nickname,user.avatar,mood.hfnum FROM response LEFT JOIN `user` ON response.who = `user`.id LEFT JOIN mood ON mood.id=response.tocomment  WHERE tocomment=:tocomment ORDER BY response.datetime DESC limit :datastart,:datanum");
        $statement->bindValue(':tocomment', $tocomment);
        $statement->bindValue(':datastart', $start, $pdo::PARAM_INT);
        $statement->bindValue(':datanum', $size, $pdo::PARAM_INT);
        $statement->setFetchMode(\PDO::FETCH_ASSOC);
        $statement->execute();
        $data = $statement->fetchAll();

        if (!$inTransaction) {
            $pdo->commit();
        }
        $extra = array(
            'totalPage' => $page['totalPage'],
            'pageSize' => $page['pageSize']
        );
        JSON::json($data, 200, $extra);
    }

    public static function GetAllResponse($towho, $page = array('*'))
    {
        global $pdo;
        $start = $page['dataStart'];
        if ($start == null) {
            $start = 0;
        }
        $size = $page['pageSize'];
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statement = $pdo->prepare("SELECT response.*,user.username,user.nickname,user.avatar FROM response LEFT JOIN `user` ON response.who = `user`.id LEFT JOIN mood ON response.tocomment= mood.id WHERE (response.towho=:towho||mood.from =:towho)&& response.who !=:towho  ORDER BY response.datetime DESC limit :datastart,:datanum");
        $statement->bindValue(':towho', $towho);
        $statement->bindValue(':datastart', $start, $pdo::PARAM_INT);
        $statement->bindValue(':datanum', $size, $pdo::PARAM_INT);
        $statement->setFetchMode(\PDO::FETCH_ASSOC);
        $statement->execute();
        $data = $statement->fetchAll();

        if (!$inTransaction) {
            $pdo->commit();
        }
        $extra = array(
            'totalPage' => $page['totalPage'],
            'pageSize' => $page['pageSize']
        );
        JSON::json($data, 200, $extra);
    }

    public static function GetWhoResponse($towho)
    {
        global $pdo;
        $inTransaction = $pdo->inTransaction();//判断事务开启
        if (!$inTransaction) {
            $pdo->beginTransaction();//开启事务
        }
        $statement = $pdo->prepare("SELECT response.*,user.username,user.nickname,user.avatar FROM response LEFT JOIN `user` ON response.who = `user`.id WHERE response.id=?");
        $statement->bindValue(1, $towho);
        $statement->setFetchMode(\PDO::FETCH_ASSOC);
        $statement->execute();
        $result = $statement->fetchAll();
        if (!$inTransaction) {
            $pdo->commit();
        }
        JSON::json($result);
    }

    /**
     * 测重
     * */
    public function isVerify()
    {
        global $pdo;
        $statament = $pdo->prepare('SELECT COUNT(*) FROM response WHERE who=?&&content=?&&tocomment=?&&datetime=?&&towho=?');
        $statament->bindValue(1, $this->who);
        $statament->bindValue(2, $this->content);
        $statament->bindValue(3, $this->tocomment);
        $statament->bindValue(4, $this->datetime);
        $statament->bindValue(5, $this->towho);
        $flag = $statament->fetchColumn();
        if ($flag == 0) {
            return false;
        } else {
            return true;
        }
    }
}